{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.14 \u5c55\u5f00\u5d4c\u5957\u7684\u5e8f\u5217\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u4e00\u4e2a\u591a\u5c42\u5d4c\u5957\u7684\u5e8f\u5217\u5c55\u5f00\u6210\u4e00\u4e2a\u5355\u5c42\u5217\u8868"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u5199\u4e00\u4e2a\u5305\u542b yield from \u8bed\u53e5\u7684\u9012\u5f52\u751f\u6210\u5668\u6765\u8f7b\u677e\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import Iterable\n\ndef flatten(items, ignore_types=(str, bytes)):\n    for x in items:\n        if isinstance(x, Iterable) and not isinstance(x, ignore_types):\n            yield from flatten(x)\n        else:\n            yield x\n\nitems = [1, 2, [3, 4, [5, 6], 7], 8]\n# Produces 1 2 3 4 5 6 7 8\nfor x in flatten(items):\n    print(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c isinstance(x, Iterable) \u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u662f\u53ef\u8fed\u4ee3\u7684\u3002\n\u5982\u679c\u662f\u7684\u8bdd\uff0c yield from \u5c31\u4f1a\u8fd4\u56de\u6240\u6709\u5b50\u4f8b\u7a0b\u7684\u503c\u3002\u6700\u7ec8\u8fd4\u56de\u7ed3\u679c\u5c31\u662f\u4e00\u4e2a\u6ca1\u6709\u5d4c\u5957\u7684\u7b80\u5355\u5e8f\u5217\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u989d\u5916\u7684\u53c2\u6570 ignore_types \u548c\u68c0\u6d4b\u8bed\u53e5 isinstance(x, ignore_types)\n\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6392\u9664\u5728\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5916\uff0c\u9632\u6b62\u5c06\u5b83\u4eec\u518d\u5c55\u5f00\u6210\u5355\u4e2a\u7684\u5b57\u7b26\u3002\n\u8fd9\u6837\u7684\u8bdd\u5b57\u7b26\u4e32\u6570\u7ec4\u5c31\u80fd\u6700\u7ec8\u8fd4\u56de\u6211\u4eec\u6240\u671f\u671b\u7684\u7ed3\u679c\u4e86\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "items = ['Dave', 'Paula', ['Thomas', 'Lewis']]\nfor x in flatten(items):\n    print(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bed\u53e5 yield from \u5728\u4f60\u60f3\u5728\u751f\u6210\u5668\u4e2d\u8c03\u7528\u5176\u4ed6\u751f\u6210\u5668\u4f5c\u4e3a\u5b50\u4f8b\u7a0b\u7684\u65f6\u5019\u975e\u5e38\u6709\u7528\u3002\n\u5982\u679c\u4f60\u4e0d\u4f7f\u7528\u5b83\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u5fc5\u987b\u5199\u989d\u5916\u7684 for \u5faa\u73af\u4e86\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def flatten(items, ignore_types=(str, bytes)):\n    for x in items:\n        if isinstance(x, Iterable) and not isinstance(x, ignore_types):\n            for i in flatten(x):\n                yield i\n        else:\n            yield x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u53ea\u6539\u4e86\u4e00\u70b9\u70b9\uff0c\u4f46\u662f yield from \u8bed\u53e5\u770b\u4e0a\u53bb\u611f\u89c9\u66f4\u597d\uff0c\u5e76\u4e14\u4e5f\u4f7f\u5f97\u4ee3\u7801\u66f4\u7b80\u6d01\u6e05\u723d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u63d0\u5230\u7684\u5bf9\u4e8e\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u7684\u989d\u5916\u68c0\u67e5\u662f\u4e3a\u4e86\u9632\u6b62\u5c06\u5b83\u4eec\u518d\u5c55\u5f00\u6210\u5355\u4e2a\u5b57\u7b26\u3002\n\u5982\u679c\u8fd8\u6709\u5176\u4ed6\u4f60\u4e0d\u60f3\u5c55\u5f00\u7684\u7c7b\u578b\uff0c\u4fee\u6539\u53c2\u6570 ignore_types \u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c yield from \u5728\u6d89\u53ca\u5230\u57fa\u4e8e\u534f\u7a0b\u548c\u751f\u6210\u5668\u7684\u5e76\u53d1\u7f16\u7a0b\u4e2d\u626e\u6f14\u7740\u66f4\u52a0\u91cd\u8981\u7684\u89d2\u8272\u3002\n\u53ef\u4ee5\u53c2\u800312.12\u5c0f\u8282\u67e5\u770b\u53e6\u5916\u4e00\u4e2a\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}